Apache Tomcat Servlet/JSP 容器

Apache Tomcat 5.5 Servlet/JSP 容器

Jaxmao Logo

链接

用户手册

参考

Apache Tomcat 开发

Apache Tomcat 5.5 Servlet/JSP 容器

Monitoring and Managing Tomcat

Printer Friendly Version
print-friendly
version
前言

监视服务器运行状况对于今天的网路来说已经变得越来越重要。监视服务器的运行,获取统计资料, 修改服务器设置是系统管理员的日常工作。

激活 JMX 遥控器

下列Sun的网站包括了一个 Java 5 版本里 设置 JMX 遥控器的选项: http://java.sun.com/j2se/1.5.0/docs/guide/management/agent.html.

这里给你的是一个快速简单的安装步骤:

在你的 Tomcat 的启动程序里加入以下几行:

    set CATALINA_OPTS="-Dcom.sun.management.jmxremote \
    -Dcom.sun.management.jmxremote.port=%my.jmx.port% \
    -Dcom.sun.management.jmxremote.ssl=false \
    -Dcom.sun.management.jmxremote.authenticate=false"
    

  1. 如果你觉得有必要验证使用者的用户名和密码,加入以下几行:
        -Dcom.sun.management.jmxremote.authenticate=true \
        -Dcom.sun.management.jmxremote.password.file=../conf/jmxremote.password \
        -Dcom.sun.management.jmxremote.access.file=../conf/jmxremote.access \
        
  2. 修改文件 $CATALINA_BASE/conf/jmxremote.access :
    monitorRole readonly
    controlRole readwrite
        
  3. 修改密码文件 $CATALINA_BASE/conf/jmxremote.password :
    monitorRole tomcat
    controlRole tomcat
        
    注意: 密码文件一定要只允许读取,防止以外修改,还要删除其他用户对这个文件操作的许可。
注意: JSR 160 JMX-Adaptor 会打开另一个数据协议的端口, 如果你安装本地防火墙的话, 这会产生问题。

用 Java 1.4 激活 JMX MX4J Http Adaptor:

  1. 安装 Tomcat 的 compat 版
  2. 把 mx4j-tools.jar 放入 common/lib。 请使用与你安装的 Tomcat一样版本的 MX4j
  3. 在 AJP 连接器中加入 MX4J JMX HTTP Adaptor 设置

          <Connector port="${AJP.PORT}" 
                handler.list="mx" 
                mx.enabled="true" 
                mx.httpHost="${JMX.HOST}"
                mx.httpPort="${JMX.PORT}"
                protocol="AJP/1.3" />
          

    提示: 如果选择 ${AJP.PORT}=0, ajp 连接器不会在那里启动。

    注意: MX4J JSR 160 RMI Adaptor to support JDK 1.4 currently not integrated.

  4. 启动 Tomcat 然后用浏览器查看: http://${JMX.HOST}:${JMX.PORT}
  5. 如果使用 mx connector 属性 mx.authMode="basic" mx.authUser="tomcat" mx.authPassword="strange" 你可以控制用户登陆。
  6. 你可以在下面网站找到完整的 Tomcat 核心 MBean 的列表: http://tomcat.apache.org/tomcat-5.5-doc/catalina/funcspecs/mbean-names.html

使用 JMX 遥控器的 Ant 指令来管理 Tomcat

Tomcat 提供了使用 Ant 1.6 来管理 Tomcat 的 antlib。

antlib 把 $CATALINA_HOME/server/lib 里的 catalina-ant.jar from 复制到 $ANT_HOME/lib 里。

下面的例子显示 JMX Accessor 的使用方法:

<project name="Catalina Ant JMX" 
        xmlns:jmx="antlib:org.apache.catalina.ant.jmx" 
        default="state"
        basedir=".">
    <property name="jmx.server.name" value="localhost" />
    <property name="jmx.server.port" value="9012" />
    <property name="cluster.server.address" value="192.168.1.75" />
    <property name="cluster.server.port" value="9025" />
 
    <target name="state" description="Show JMX Cluster state">
        <jmx:open
            host="${jmx.server.name}"
            port="${jmx.server.port}"
            username="controlRole"
            password="tomcat"/>
        <jmx:get
            name="Catalina:type=IDataSender,host=localhost,senderAddress=${cluster.server.address},senderPort=${cluster.server.port}" 
            attribute="connected"
            resultproperty="IDataSender.backup.connected"
            echo="false"
        />
       <jmx:get
            name="Catalina:type=ClusterSender,host=localhost" 
            attribute="senderObjectNames"
            resultproperty="senderObjectNames"
            echo="false"
        />
        <!-- get current maxActiveSession from ClusterTest application
             echo it to ant output and store at 
             property <em>clustertest.maxActiveSessions.orginal</em>
        -->
       <jmx:get
            name="Catalina:type=Manager,path=/ClusterTest,host=localhost" 
            attribute="maxActiveSessions"
            resultproperty="clustertest.maxActiveSessions.orginal"
            echo="true"
        />
        <!-- set maxActiveSession to 100
        -->
        <jmx:set
            name="Catalina:type=Manager,path=/ClusterTest,host=localhost" 
            attribute="maxActiveSessions"
            value="100"
            type="int"
        />
        <!-- get all sessions and split result as delimiter <em>SPACE</em> for easy
             access all session ids directly with ant property sessions.[0..n].
        -->
        <jmx:invoke
            name="Catalina:type=Manager,path=/ClusterTest,host=localhost" 
            operation="listSessionIds"
            resultproperty="sessions"
            echo="false"
            delimiter=" "
        />
        <!-- Access session attribute <em>Hello</em> from first session.
        -->
        <jmx:invoke
            name="Catalina:type=Manager,path=/ClusterTest,host=localhost" 
            operation="getSessionAttribute"
            resultproperty="Hello"
            echo="false"
        >
          <arg value="${sessions.0}"/>
          <arg value="Hello"/>
        </jmx:invoke> 
        <!-- Query for all application manager.of the server from all hosts
             and bind all attributes from all found manager mbeans.
        -->
        <jmx:query
            name="Catalina:type=Manager,*" 
            resultproperty="manager"
            echo="true"
            attributebinding="true"
        />
        <!-- echo the create properties -->
        <echo>
           senderObjectNames: ${senderObjectNames.0}
           IDataSender.backup.connected: ${IDataSender.backup.connected}
           session: ${sessions.0}
           manager.length: ${manager.length}
           manager.0.name: ${manager.0.name}
           manager.1.name: ${manager.1.name}
           hello: ${Hello}
           manager.ClusterTest.0.name: ${manager.ClusterTest.0.name}
           manager.ClusterTest.0.activeSessions: ${manager.ClusterTest.0.activeSessions}
           manager.ClusterTest.0.counterSend_EVT_SESSION_EXPIRED: ${manager.ClusterTest.0.counterSend_EVT_SESSION_EXPIRED}
           manager.ClusterTest.0.counterSend_EVT_GET_ALL_SESSIONS: ${manager.ClusterTest.0.counterSend_EVT_GET_ALL_SESSIONS}
        </echo>   

    </target>
 
</project>
   

导入: 用下面的指令导入 JMX Accessor 项目 <import file="${CATALINA.HOME}/bin/jmxaccessor-tasks.xml" /> , 参考下面的 Ant 指令: jmxOpen, jmxSet, jmxGet, jmxQuery, jmxInvoke,jmxEquals and jmxCondition.

JMXAccessorOpenTask - jmx 打开连接指令

属性
属性 描述 缺省值
url 设置 jmx 连接 url - service:jmx:rmi:///jndi/rmi://localhost:8050/jmxrmi
<Host> 设置主机名,简化冗长的 url 格式 localhost
port 设置遥控连接的端口 8050
username 遥控器用户名
password jmx 遥控器用户连接密码
ref Name of the internal connection referenz. With this attribute you can configure more the one connection inside the same ant projekt. jmx.server
echo Echo the command usage (for analyse access or debugging) false
if Only execute if a property of the given name exists in the current project.
unless Only execute if a property of the given name not exists in the current project.

实例:jmx 连接

    <jmx:open
            host="${jmx.server.name}"
            port="${jmx.server.port}"
    />

实例:用URL、用户名和密码打开 jmx 连接

    <jmx:open
            url="service:jmx:rmi:///jndi/rmi://localhost:9024/jmxrmi"
            ref="jmx.server.9024"
            username="controlRole"
            password="tomcat"    
    />

实例:当jmx.if 存在而且jmx.unless不存在,用URL、用户名和密码打开 jmx 连接

    <jmx:open
            url="service:jmx:rmi:///jndi/rmi://localhost:9024/jmxrmi"
            ref="jmx.server.9024"
            username="controlRole"
            password="tomcat"    
            if="jmx.if"    
            unless="jmx.unless"    
    />

注意jmxOpen任务的属性同样适用于其他任务或条件。

JMXAccessorGetTask: get attribute value ant task

List of Attributes
属性 描述 Default value
name Full qualified JMX ObjectName -- Catalina:type=Server
attribute Existing Mbean attribute (see Tomcat mbean description above)
ref JMX Connection reference jmx.server
echo Echo command usage (access and result) false
resultproperty Save result at this project property
delimiter Split result with delimiter (java.util.StringTokenizier) and use resultproperty as prefix to store tokens.
separatearrayresults When return value is an array, save result as property list ($resultproperty.[0..N] and $resultproperty.lenght) true

Example to get remote mbean attribute from default jmx connection

    <jmx:get
        name="Catalina:type=Manager,path=/servlets-examples,host=localhost" 
        attribute="maxActiveSessions"
        resultproperty="servlets-examples.maxActiveSessions"
    />

Example to get and result array and split it at separate properties

    <jmx:get
        name="Catalina:type=ClusterSender,host=localhost" 
        attribute="senderObjectNames"
        resultproperty="senderObjectNames"
    />
Access the senderObjectNames properties with:
    ${senderObjectNames.lenght} give the number of returned sender list.
    ${senderObjectNames.[0..N]} found all sender object names

Example to get IDataSender attribute connected only when cluster is configured.

<jmx:query
    failonerror="false"
    name="Catalina:type=Cluster,host=${tomcat.application.host}"
    resultproperty="cluster"
/>
<jmx:get
    name="Catalina:type=IDataSender,host=${tomcat.application.host},senderAddress=${cluster.backup.address},senderPort=${cluster.backup.port}" 
    attribute="connected"
    resultproperty="datasender.connected"
    if="cluster.0.name" />

JMXAccessorSetTask: set attribute value ant task

List of Attributes
属性 描述 Default value
name Full qualified JMX ObjectName -- Catalina:type=Server
attribute Existing Mbean attribute (see Tomcat mbean description above)
value value that set to attribute
type type of the attribute. java.lang.String
ref JMX Connection reference jmx.server
echo Echo command usage (access and result) false

Example to set remote mbean attribute value

    <jmx:set
        name="Catalina:type=Manager,path=/servlets-examples,host=localhost" 
        attribute="maxActiveSessions"
        value="500"
        type="int"
    />

Examples to set multiple remote mbean attributes

    <jmx:set
        name="java.lang:type=Threading"> 
			 <arg name="ThreadContentionMonitoringEnabled" value="true" type="boolean"/>
			 <arg name="ThreadCpuTimeEnabled" value="false" type="boolean"/>
    </jmx:set>

JMXAccessorInvokeTask: invoke Mbean operation ant task

List of Attributes
属性 描述 Default value
name Full qualified JMX ObjectName -- Catalina:type=Server
operation Existing Mbean operation (see Tomcat http://tomcat.apache.org/tomcat-5.5-doc/catalina/funcspecs/fs-admin-opers.html.
ref JMX Connection reference jmx.server
echo Echo command usage (access and result) false
resultproperty Save result at this project property
delimiter Split result with delimiter (java.util.StringTokenizier) and use resultproperty as prefix to store tokens.
separatearrayresults When return value is an array, save result as property list ($resultproperty.[0..N] and $resultproperty.lenght) true

stop an application

    <jmx:invoke
        name="Catalina:type=Manager,path=/servlets-examples,host=localhost" 
        operation="stop"/>
Now you can find the sessionid at ${sessions.[0..N} properties and access the count with ${sessions.lenght} property.

Example to get all sessionids

    <jmx:invoke
        name="Catalina:type=Manager,path=/servlets-examples,host=localhost" 
        operation="listSessionIds"
        resultproperty="sessions"
        delimiter=" "        
    />
Now you can find the sessionid at ${sessions.[0..N} properties and access the count with ${sessions.lenght} property.

Example to get remote mbean session attribute from session ${sessionid.0}

    <jmx:invoke
        name="Catalina:type=Manager,path=/ClusterTest,host=localhost" 
        operation="getSessionAttribute"
        resultproperty="hello">
         <arg value="${sessionid.0}"/>
         <arg value="Hello" />
 </jmx:invoke>

Example to create a new access logger valve at vhost localhost

 <jmx:invoke
         name="Catalina:type=MBeanFactory" 
         operation="createAcccesLoggerValve"
         resultproperty="acccesLoggerObjectName"
 >
     <arg value="Catalina:type=Host,host=localhost"/>
 </jmx:invoke>
Now you can find new Mbean with name stored at ${acccesLoggerObjectName} proeprty.

JMXAccessorQueryTask: query Mbean ant task

List of Attributes
属性 描述 Default value
name JMX ObjectName query string -- Catalina:type=Manager,*
ref JMX Connection reference jmx.server
echo Echo command usage (access and result) false
resultproperty Prefix project property name to all founded Mbeans (mbeans.[0..N].objectname)
attributebinduing bind ALL MBean attributes in addition to name false
delimiter Split result with delimiter (java.util.StringTokenizier) and use resultproperty as prefix to store tokens.
separatearrayresults When return value is an array, save result as property list ($resultproperty.[0..N] and $resultproperty.lenght) true

Get all Manager ObjectNames from all services and Hosts

  <jmx:query
           name="Catalina:type=Manager,* 
           resultproperty="manager" />
Now you can find the Session Manager at ${manager.[0..N].name} properties and access the result object counter with ${manager.length} property.

Example to get the Manager from servlet-examples application an bind all mbean properties

  <jmx:query
           name="Catalina:type=Manager,path=/servlet-examples,host=localhost*" 
           attributebinding="true"
           resultproperty="manager.servletExamples" />
Now you can find the manager at ${manager.servletExamples.0.name} property and can access all properties from this manager with ${manager.servletExamples.0.[manager attribute names]}. The result object counter from MBeans is stored ad ${manager.length} property.

Example to get all MBeans from a server and store inside an external xml property file

<project name="jmx.query"         
            xmlns:jmx="antlib:org.apache.catalina.ant.jmx"
            default="query-all" basedir=".">
<property name="jmx.host" value="localhost"/>
<property name="jmx.port" value="8050"/>
<property name="jmx.username" value="controlRole"/>
<property name="jmx.password" value="tomcat"/>

<target name="query-all" description="Query all MBeans of a server">
<!-- Configure connection -->
<jmx:open 
    host="${jmx.host}"
    port="${jmx.port}"
    ref="jmx.server"
    username="${jmx.username}"
    password="${jmx.password}"/>
<!-- Query MBean list -->
<jmx:query 
    name="*:*"
    resultproperty="mbeans"
    attributebinding="false"/>
    
<echoproperties
    destfile="mbeans.properties"
    prefix="mbeans."
    format="xml"/>
    
<!-- Print results -->
<echo
    message="Number of MBeans in server ${jmx.host}:${jmx.port} is ${mbeans.length}"/>
</target>
</project>
Now you can find all MBeans inside the file mbeans.properties.

JMXAccessorCreateTask: remote create mbean ant task

List of Attributes
属性 描述 Default value
name(名称) Full qualified JMX ObjectName -- Catalina:type=MBeanFactory
className Existing MBean full qualified classname (see Tomcat mbean description above)
classLoader ObjectName of server or web application classloader
( Catalina:type=ServerClassLoader,name=[server,common,shared] or
Catalina:type=WebappClassLoader,path=/myapps,host=localhost)
ref JMX Connection reference jmx.server
echo Echo command usage (access and result) false

Example to create remote mbean

    <jmx:create
             ref="${jmx.reference}"
             name="Catalina:type=MBeanFactory"
             className="org.apache.commons.modeler.BaseModelMBean"
             classLoader="Catalina:type=ServerClassLoader,name=server">             
             <Arg value="org.apache.catalina.mbeans.MBeanFactory" />
    </jmx:create> 

Warning: A lot of tomcat mbeans can't be really create and connect with
the parent. The valve, cluster or realm Mbeans are not autconnect with there parent.
Use MBeanFacrory create operation instead.

JMXAccessorUnregisterTask: remote unregister mbean ant task

List of Attributes
属性 描述 Default value
name Full qualified JMX ObjectName -- Catalina:type=MBeanFactory
ref JMX Connection reference jmx.server
echo Echo command usage (access and result) false

Example to unregister remote mbean

    <jmx:unregister
        name="Catalina:type=MBeanFactory" 
    />

Warning: A lot of tomcat mbeans can't be really unregister.
The Mbeans are not deregister from parent. Use MBeanFacrory
remove operation instead.

JMXAccessorCondition: express condition

List of Attributes
属性 描述 Default value
url Set jmx connection url - service:jmx:rmi:///jndi/rmi://localhost:8050/jmxrmi
host Set the host, shortcut the very long url syntax. localhost
port Set the remote connection port 8050
username remote jmx connection user name.
password remote jmx connection password.
ref Name of the internal connection reference. With this attribute you can configure more the one connection inside the same ant projekt. jmx.server
name Full qualified JMX ObjectName -- Catalina:type=Server
echo Echo condition usage (access and result) false
if Only execute if a property of the given name exists in the current project.
unless Only execute if a property of the given name not exists in the current project.
value (requiered) Second arg for operation
type Value type to express operation (support long and double) long
operation express one
  • == equals
  • != not equals
  • > greater than (&gt;)
  • >= greater than or equals (&gt;=)
  • < lesser than (&lt;)
  • <= lesser than or equals (&lt;=)
==

Wait for server connection and that cluster backup node is accessable

      <target name="wait">
         <waitfor maxwait="${maxwait}" maxwaitunit="second" timeoutproperty="server.timeout" >
            <and>
                <socket server="${server.name}" port="${server.port}"/>
                <http url="${url}"/>
                <jmx:condition
                    operation="==" 
                    host="localhost" 
                    port="9014"
                    username="controlRole"
                    password="tomcat"
                    name="Catalina:type=IDataSender,host=localhost,senderAddress=192.168.111.1,senderPort=9025"
                    attribute="connected"
                    value="true"
                />
            </and>
        </waitfor>
        <fail if="server.timeout" message="Server ${url} don't answer inside ${maxwait} sec" />
        <echo message="Server ${url} alive" />
    </target>

JMXAccessorEqualsCondition: equals Mbean ant condition

List of Attributes
属性 描述 Default value
url Set jmx connection url - service:jmx:rmi:///jndi/rmi://localhost:8050/jmxrmi
host Set the host, shortcut the very long url syntax. localhost
port Set the remote connection port 8050
username remote jmx connection user name.
password remote jmx connection password.
ref Name of the internal connection referenz. With this attribute you can configure more the one connection inside the same ant projekt. jmx.server
name Full qualified JMX ObjectName -- Catalina:type=Server
echo Echo condition usage (access and result) false

Wait for server connection and that cluster backup node is accessable

      <target name="wait">
         <waitfor maxwait="${maxwait}" maxwaitunit="second" timeoutproperty="server.timeout" >
            <and>
                <socket server="${server.name}" port="${server.port}"/>
                <http url="${url}"/>
                <jmx:equals 
                    host="localhost" 
                    port="9014"
                    username="controlRole"
                    password="tomcat"
                    name="Catalina:type=IDataSender,host=localhost,senderAddress=192.168.111.1,senderPort=9025"
                    attribute="connected"
                    value="true"
                />
            </and>
        </waitfor>
        <fail if="server.timeout" message="Server ${url} don't answer inside ${maxwait} sec" />
        <echo message="Server ${url} alive" />
    </target>


Copyright © 1999-2006, Apache Software Foundation